<?php
/**
 * @author    : Death-Satan
 * @date      : 2021/9/21
 * @createTime: 17:53
 * @company   : Death撒旦
 * @link      https://www.cnblogs.com/death-satan
 */

namespace SaTan\Dcat\Extensions\ComposerViewer\Http\Repositories;

use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Alert;
use Illuminate\Support\Collection;
use SaTan\Dcat\Extensions\ComposerViewer\DcatComposerViewerServiceProvider;

class Composer implements \Dcat\Admin\Contracts\Repository
{

    /**
     * @inheritDoc
     */
    public function getKeyName()
    {
        return 'package_name';
    }

    /**
     * @inheritDoc
     */
    public function getCreatedAtColumn()
    {
        return 'created_at';
    }

    /**
     * @inheritDoc
     */
    public function getUpdatedAtColumn()
    {
        return 'updated_at';
    }

    protected function trans($key, $replace = [], $locale = null)
    {
        return DcatComposerViewerServiceProvider::trans('repositories.'.$key,$replace,$locale); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritDoc
     */
    public function isSoftDeletes()
    {
        return false;
    }

    /**
     * 获取classLoader
     * @return Collection
     */
    protected function dataAll()
    {
        $data = InstalledVersions::getAllRawData();
        if (count($data)>=1) {
            $versions =  collect($data[0]['versions'])
                            ->map(function ($value,$item){

                                $value['package_name'] = $item;
                                if (!empty($value['replaced']))
                                {
                                    $value['pretty_version'] = $value['replaced'][0];
                                    $value['type'] = $value['type']??'library';
                                    $value['install_path'] = $value['install_path']??base_path();
                                }
                                return $value;
                            });
            return $versions->values();
        }

        \Admin::exit(Alert::make()
            ->title($this->trans('errors.'.__FUNCTION__.'.title'))
            ->content($this->trans('errors.'.__FUNCTION__.'.content'))
        );
    }

    /**
     * @inheritDoc
     */
    public function get(Grid\Model $model)
    {
        $data =$this->dataAll();

        // 获取当前页数
        $currentPage = (int)$model->getCurrentPage();
        // 获取每页显示行数
        $perPage = (int)$model->getPerPage();

        $package_name = $model->filter()->input('package_name');

        if ($package_name){
        $data = $data->filter(function ($datum)use ($package_name){
//            dump($datum['package_name'],$package_name,$datum['package_name']==$package_name);
            if ($datum['package_name']==$package_name)
            {
                return true;
            }
            return !(strstr($datum['package_name'], $package_name) === false);
        });
        }

        $chunk = $data->chunk($perPage)->all();

        /**
         * @var $current_data Collection
         */
        $current_data = $chunk[$currentPage-1]??collect();
        return $model->makePaginator(
            $data->count() ?? 0, // 传入总记录数
            $current_data->all() ?? [] // 传入数据二维数组
        );;
    }

    /**
     * @inheritDoc
     */
    public function edit(Form $form)
    {
        return $form->response()->error($this->trans('errors.'.__FUNCTION__.'.message'));
    }

    /**
     * @inheritDoc
     */
    public function detail(Show $show)
    {
        // TODO: Implement detail() method.
    }

    /**
     * @inheritDoc
     */
    public function store(Form $form)
    {
        return $form->response()->error($this->trans('errors.'.__FUNCTION__.'.message'));
    }

    /**
     * @inheritDoc
     */
    public function updating(Form $form)
    {
        \Admin::exit(Alert::make()->content($this->trans('errors.message')));
    }

    /**
     * @inheritDoc
     */
    public function update(Form $form)
    {
        return $form->response()->error($this->trans('errors.'.__FUNCTION__.'.message'));
    }

    /**
     * @inheritDoc
     */
    public function delete(Form $form, array $deletingData)
    {
        return $form->response()->error($this->trans('errors.'.__FUNCTION__.'.message'));
    }

    /**
     * @inheritDoc
     */
    public function deleting(Form $form)
    {
        \Admin::exit(Alert::make()->content($this->trans('errors.message')));
    }
}
